package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.comment.reqeust.ReqUserCollect;
import com.quanyan.comment.serviceFacade.UserCollectServiceFacade;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.user.constants.UserConstants;
import com.quanyan.user.model.vo.request.ReqUserCollectDel;
import com.quanyan.user.model.vo.request.ReqUserCollectDelList;
import com.quanyan.user.model.vo.request.ReqUserCollectList;
import com.quanyan.user.model.vo.response.RespActivityCollect;
import com.quanyan.user.service.ActivityCollectService;
import com.quanyan.user.service.UserCollectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jian.zhou on 2016/6/28.
 * 用户收藏
 */
@RestController
@RequestMapping(value = "/api/user/collection")
public class ActivityCollectController {

    private static final Logger logger = LoggerFactory.getLogger(ActivityCollectController.class);

    @Autowired
    private ActivityCollectService activityCollectService;

    @Autowired
    private UserCollectService userCollectService;

    /**
     * 我的收藏列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public APIResponse collectList(@RequestBody Map map,HttpServletRequest request) {
        logger.info("star query user collect list");
        int uid = (int)request.getAttribute("uid");
        Integer pageSize = map.get("pageSize") == null ? 10:Integer.parseInt(map.get("pageSize").toString());
        Integer pageNo = map.get("pageNum") == null ? 0:Integer.parseInt(map.get("pageNum").toString());
        try {
            PageObj<List<RespActivityCollect>> lst = activityCollectService.queryActivityCollect(uid, pageNo, pageSize);
            return APIResponse.returnSuccess(lst);
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("查询活动列表异常！");
        }
    }

    /**
     *删除我的收藏
     * */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public APIResponse collectDelete(HttpServletRequest request,@RequestBody @Valid ReqUserCollectDelList ids){
        logger.info("star delete user collect");

        int uid = (int)request.getAttribute("uid");

        if(ids == null || ids.getIds().size() < 1)
            return APIResponse.returnFail("reqUserCollectDel不能为空！");

        logger.info("star delete user collect  param is : {}",ids.toString());
        List<Integer> clubIds = new ArrayList<Integer>();
        List<Integer> outdoorIds = new ArrayList<Integer>();

       for(ReqUserCollectDel r: ids.getIds()){
           if(r.getCollectionType().equals(UserConstants.ACTIVITY_TYPE_CLUB))
               clubIds.add(r.getCid());
           else if(r.getCollectionType().equals(UserConstants.ACTIVITY_TYPE_OUTDOOR)){
              outdoorIds.add(r.getCid());
           }

       }

        if(clubIds.size() > 0 ){
            ReqUserCollect reqUserCollect = new ReqUserCollect();
            reqUserCollect.setUserId(uid);
            reqUserCollect.setStatus(0);
            reqUserCollect.setSystemCode(Constants.ACTIVITY_SYSTEM_CODE);
            reqUserCollect.setBusinessCode(Constants.CLUB_COLLECT_ACTIVITY_BUSINESS_CODE);
            userCollectService.cancelUserCollect(reqUserCollect,clubIds);
        }
        if( outdoorIds.size() > 0){
            ReqUserCollect reqUserCollect1 = new ReqUserCollect();
            reqUserCollect1.setUserId(uid);
            reqUserCollect1.setStatus(0);
            reqUserCollect1.setSystemCode(Constants.SYSTEM_OUTDOOR);
            reqUserCollect1.setBusinessCode(Constants.BUSINESS_OUTDOOR_CM);
            userCollectService.cancelUserCollect(reqUserCollect1, outdoorIds);
        }

        return APIResponse.returnSuccess();
    }
}
